package com.st.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.st.entity.*;
import com.st.mapper.IContentMapper;
import com.st.service.IContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


@Service
public class ContentServiceImpl extends ServiceImpl<IContentMapper,Content> implements IContentService {

    @Autowired
    private IContentMapper iContentMapper;

    //查询所有内容
    @Override
    public PageResult<Content> search(PageDTO<Content> pageDTO) {
        if (pageDTO == null) {
            return new PageResult(false,"参数异常");
        }
        Content content = pageDTO.getData();
        IPage<Content> iPage = new Page<>(pageDTO.getPage(),pageDTO.getLimit());
        IPage<Content> pageInfo = iContentMapper.findById(iPage, content);
        PageResult pageResult = new PageResult();
        pageResult.setData(pageInfo.getRecords());
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setSuccess(true);
        pageResult.setMsg("查询成功");
        return pageResult;
    }
    //添加修改内容
    @Override
    public Result addContent(Content content) {
        if(content==null){
            return new Result(false,"参数异常");
        }
        if(StringUtils.isEmpty(content.getId())){
            int i = iContentMapper.insert(content);
            return new Result(i==1?true:false,i==1?"添加成功":"添加失败");
        }else {
            int i = iContentMapper.updateById(content);
            return new Result(i==1?true:false,i==1?"修改成功":"修改失败");
        }
    }
    //删除内容
    @Override
    public Result delContent(Content content) {
        if (content == null) {
            return new Result(false,"参数异常");
        }
        int i = iContentMapper.deleteById(content.getId());
        return new Result(i==1?true:false,i==1?"删除成功":"删除失败");
    }
}
